﻿//创建时间: 2010-10-11
//创建作者: 李 城
//功能说明: 内容模型【字段】  单个字段的修改 添加 
//最后修改: 2010-10-11
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.Common;
using TRPOP.CMS.Web.UI;


using TRPOP.CMS.Business;
using TRPOP.CMS.Model;

namespace TRPOP.CMS.Web.Admin
{
    public partial class Model_field : BaseAdmin
    {

        #region 当前页面权限验证
        protected override void SubPageInit()
        {
            base.m_SubPagePermission = Plugin.PermissionKeys.PermissionEnum.SystemManage_ContentModel;
        }
        #endregion


        #region 页面变量
        protected bool m_IsAdd = true;
        protected int m_cid = 0;
        protected int m_id = 0;
        protected string m_Title = "";
        protected ModelInfo m_ModelInfo = new ModelInfo();
        protected ModelFieldInfo m_ModelFieldInfo = new ModelFieldInfo();
        protected ModelManage m_ModelManage = new ModelManage();
        protected ModelFieldManage m_ModelFieldManage = new ModelFieldManage();
        protected string m_strRefer = "Model_save.aspx?id={0}";
        #endregion

        #region 页面事件
        protected void Page_Load(object sender, EventArgs e)
        {
            base.AlertForNoPermissionControl(TRPOP.CMS.Plugin.VersionLimit.ContentModelEdit);
            if (!IsPostBack)
            {
                LoadBase();
                BindData();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strMSG = "标识 、表字段名";
            if (!PageAssist.CheckForm(txtName, ref strMSG) ||
                !PageAssist.CheckForm(txtFieldName, ref strMSG)
                )
            {
                Alert(strMSG);
                return;
            }
            m_ModelInfo = (ModelInfo)GetPageViewState("modelinfo");
            m_ModelFieldInfo = (ModelFieldInfo)GetPageViewState("modelfield");
            if (m_ModelFieldInfo == null)
            {
                m_ModelFieldInfo = new ModelFieldInfo();
            }
            m_ModelFieldInfo.ModelId = m_ModelInfo.ModelId;
            m_ModelFieldInfo.Name = txtName.TextFilterSQL.Trim().RemoveHtml();
            m_ModelFieldInfo.FieldName = txtFieldName.TextFilterSQL.Trim().RemoveHtml();
            m_ModelFieldInfo.FieldTypeStr =  ddlFieldTypeStr.SelectedValue ;
            m_ModelFieldInfo.FieldLength = txtFieldLength.TextTrim.ToInt();
            m_ModelFieldInfo.DefaultValue = txtDefaultValue.TextFilterSQL.Trim().RemoveHtml();
            m_ModelFieldInfo.FormInputTypeId = ddlFormInputType.SelectedValue.ToInt();
            m_ModelFieldInfo.FormInputLimitTypeId = ddlFormInputLimitType.SelectedValue.ToInt();
            m_ModelFieldInfo.FormInputValueTypeId = ddlFormInputValueType.SelectedValue.ToInt();
            m_ModelFieldInfo.FieldInputPicker = ddlFieldInputPicker.SelectedValue;
            m_ModelFieldInfo.IsContribute =  chkIsContribute.Checked ;
            m_ModelFieldInfo.IsSearch = chkIsSearch.Checked;
            m_ModelFieldInfo.IsCollect = chkIsCollect.Checked;
            m_ModelFieldInfo.IsEdit = chkIsEdit.Checked;
            m_strRefer = string.Format(m_strRefer, m_ModelInfo.ModelId);

            EnumSubmitResult enumResult= m_ModelFieldManage.Save(m_ModelFieldInfo);
            if (m_ModelFieldInfo.ModelFieldId > 0)
            {
                base.SaveLog(Plugin.LogKeys.OperObject.ModelOper, Plugin.LogKeys.OperResult.ModelFieldEdit, m_ModelFieldInfo.ModelId.ToString() + ":" + m_ModelFieldInfo.ModelFieldId);
            }
            else
            {
                base.SaveLog(Plugin.LogKeys.OperObject.ModelOper, Plugin.LogKeys.OperResult.ModelFieldAdd, m_ModelFieldInfo.ModelId.ToString() + ":" + m_ModelFieldInfo.ModelFieldId);
            }

            SubmitAlertAndThisNewLocation(enumResult, "Model_save.aspx?id=" + m_ModelInfo.ModelId);
        }
        #endregion

        #region 页面方法

        public void LoadBase()
        {
            int.TryParse(InitRequestViewState("cid"), out m_cid);
            int.TryParse(InitRequestViewState("id"), out m_id);
            m_strRefer = string.Format(m_strRefer, m_cid);
            m_ModelInfo = m_ModelManage.GetInfoById(m_cid);
            if (m_cid <= 0 || m_ModelInfo == null)
            {
                Alert("请通过正确的途径进入本页面");
                return;
            }
            else
            {
                m_Title = m_ModelInfo.Name + "模型";
                InitPageViewState("modelinfo", m_ModelInfo);
            }
        }

        public void BindData()
        {
            ddlFieldTypeStr.DataSource = PageAssist.GetAllFieldType(SystemInfoHelper.getSystemInfo().AdminFolderName);
            ddlFieldTypeStr.DataBind();
            ddlFormInputType.DataSource = (new  FormInputTypeManage()).GetListByCondition(0, "", " FormInputTypeId asc");
            ddlFormInputType.DataBind();
            ddlFormInputLimitType.DataSource = (new FormInputLimitTypeManage()).GetListByCondition(0, "", "FormInputLimitTypeId asc ");
            ddlFormInputLimitType.DataBind();
            ddlFormInputValueType.DataSource = (new FormInputValueTypeManage()).GetListByCondition(0, "", " FormInputValueTypeId asc ");
            ddlFormInputValueType.DataBind();
            ddlFieldInputPicker.DataSource = PageAssist.GatAllFieldInputPicker();
            ddlFieldInputPicker.DataTextField = "value";
            ddlFieldInputPicker.DataValueField = "key";
            ddlFieldInputPicker.DataBind();
            int.TryParse(GetRequestViewState("id"), out m_id);
            if (m_id > 0)
            {
                m_IsAdd = false;
                m_ModelFieldInfo = m_ModelFieldManage.GetInfoById(m_id);
                InitPageViewState("modelfield", m_ModelFieldInfo);
                txtName.Text = m_ModelFieldInfo.Name;
                txtFieldName.Text = m_ModelFieldInfo.FieldName;
                ddlFieldTypeStr.SelectedValue = m_ModelFieldInfo.FieldTypeStr;
                txtFieldLength.Text = m_ModelFieldInfo.FieldLength.ToString();
                txtDefaultValue.Text = m_ModelFieldInfo.DefaultValue;
                ddlFormInputType.SelectedValue = m_ModelFieldInfo.FormInputTypeId.ToString();
                ddlFormInputLimitType.SelectedValue = m_ModelFieldInfo.FormInputLimitTypeId.ToString();
                ddlFormInputValueType.SelectedValue = m_ModelFieldInfo.FormInputValueTypeId.ToString();
                chkIsContribute.Checked = m_ModelFieldInfo.IsContribute;
                ddlFieldInputPicker.SelectedValue = m_ModelFieldInfo.FieldInputPicker;
                chkIsSearch.Checked = m_ModelFieldInfo.IsSearch;
                chkIsCollect.Checked = m_ModelFieldInfo.IsCollect;
                chkIsEdit.Checked = m_ModelFieldInfo.IsEdit;
                return;
            }
        }
        #endregion
    }
}
